﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Person;
using BusinessObjects.Person;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Person
{
    public class SqlServerCountryRegionDao : ICountryRegionDao
    {
        public CountryRegion GetCountryRegion(string countryRegionCode)
        {
            string procedures = "Get_CountryRegion_ByCode";

            object[] parms = { "@CountryRegionCode", countryRegionCode };
            return Db.Read(procedures, true, Make, parms);
        }

        public List<CountryRegion> GetCountryRegionsByIsActive(bool isActive)
        {
            string procedures = "Get_All_CountryRegions_ByIsActive";

            object[] parms = { "@IsActive", isActive };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<CountryRegion> GetCountryRegions()
        {
            string procedures = "Get_All_CountryRegions";

            return Db.ReadList(procedures, true, Make, null);
        }

        public List<CountryRegion> GetCountryRegionsByPage(int currentPage)
        {
            string procedures = "Get_Paged_CountryRegions";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_CountryRegion";

            return Db.ReadTotalPage(procedures, true, null);
        }

        public void InsertCountryRegion(CountryRegion countryRegion)
        {
            string sql = "Insert_CountryRegion";

            countryRegion.CountryRegionCode = Db.Insert(sql, true, Take(countryRegion));
        }

        public void UpdateCountryRegion(CountryRegion countryRegion)
        {
            string sql = "Update_CountryRegion";

            Db.Update(sql, true, Take(countryRegion));
        }

        public void DeleteCountryRegion(CountryRegion countryRegion)
        {
            string sql = "Delete_CountryRegion";

            Db.Update(sql, true, Take(countryRegion.CountryRegionCode));
        }

        private object[] Take(CountryRegion countryRegion)
        {
            return new object[]  
            {
                "@CountryRegionCode", countryRegion.CountryRegionCode,
                "@CountryRegionName", countryRegion.CountryRegionName,
                "@CreateDate", countryRegion.CreateDate,
                "@ModifiedDate", countryRegion.ModifiedDate,
                "@Description", countryRegion.Description,
                "@IsActive", countryRegion.IsActive
            };
        }

        private object[] Take(string countryRegionCode)
        {
            return new object[]  
            {
                "@CountryRegionCode", countryRegionCode,
            };
        }

        private static Func<IDataReader, CountryRegion> Make = reader =>
    new CountryRegion
    {
        CountryRegionCode = reader["CountryRegionCode"].AsString(),
        CountryRegionName = reader["CountryRegionName"].AsString(),
        CreateDate = reader["CreateDate"].AsDateTime(),
        ModifiedDate = reader["ModifiedDate"].AsDateTime(),
        Description = reader["Description"].AsString(),
        IsActive = reader["IsActive"].AsBool()
    };

    }
}
